package com.example.sandbox.controller.order.abstractController;

import com.example.sandbox.controller.order.model.TransferOrderRQ;
import com.example.sandbox.controller.order.model.TransferOrderRS;
import com.example.sandbox.dto.ApiRes;
import com.example.sandbox.dto.CS;
import com.example.sandbox.dto.MchApp;
import com.example.sandbox.dto.MchRedisContext;
import com.example.sandbox.dto.domain.TransferOrder;
import com.example.sandbox.helper.BizException;
import com.example.sandbox.mapper.TransferOrderMapper;
import com.example.sandbox.service.TransferOrderService;
import com.example.sandbox.util.StringKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 转账接口
 *
 * @date 2021/8/11 11:07
 */
@Slf4j
@Component
public class AbstractTransferOrderController {


    @Autowired
    private TransferOrderService transferOrderService;
    @Autowired
    private TransferOrderMapper transferOrderMapper;

    /**
     * 转账改造-异步请求上游
     **/
    public ApiRes transferOrderAsyncRequestUpStream(TransferOrderRQ bizRQ, MchRedisContext mchRedisContext)
            throws BizException {
        TransferOrder transferOrder = new TransferOrder();
        String mchNo = bizRQ.getMchNo();
        String ifCode = bizRQ.getIfCode();

        // 商户订单号是否重复
//        long startTime = System.currentTimeMillis();
        // 只有新订单模式，进行校验
//        if (transferOrderService.count(
//                TransferOrder.gw().eq(TransferOrder::getMchNo, mchNo).eq(TransferOrder::getMchOrderNo, bizRQ.getMchOrderNo())) > 0) {
//            throw new BizException("Merchant order[" + bizRQ.getMchOrderNo() + "]Already exists");
//        }
//        log.info("商户订单号是否重复,cost:{}", System.currentTimeMillis() - startTime);

        if (StringUtils.isNotEmpty(bizRQ.getNotifyUrl()) && !StringKit.isAvailableUrl(bizRQ.getNotifyUrl())) {
            // 异步通知地址协议仅支持http:// 和 https://
            throw new BizException(
                    "The asynchronous notification address protocol only supports http:// or https://!");
        }
        MchApp mchApp = new MchApp();
        BeanUtils.copyProperties(mchRedisContext, mchApp);
        // 入库
        String currency = bizRQ.getCurrency();
        Long amount = bizRQ.getAmount();

        transferOrder.setTransferId(bizRQ.getOrderId()); // 生成转账订单号
        transferOrder.setMchNo(mchNo); // 商户号
        transferOrder.setIsvNo("lm0000000001"); // 服务商号
        transferOrder.setAppId(mchApp.getAppId()); // 商户应用appId
        transferOrder.setMchName(mchNo); // 商户名称（简称）
        transferOrder.setMchType(CS.INFO_TYPE_MCH); // 商户类型
        transferOrder.setMchOrderNo(bizRQ.getMchOrderNo()); // 商户订单号
        transferOrder.setIfCode(ifCode); // 接口代码
        transferOrder.setWayCode("0"); // 渠道代码
        transferOrder.setEntryType(bizRQ.getEntryType()); // 入账方式
        transferOrder.setAmount(amount); // 订单金额
        transferOrder.setFee(100L); // 手续费
        transferOrder.setCurrency(currency); // 币种
        transferOrder.setClientIp("0.0.0.1"); // 客户端IP
        transferOrder.setState(TransferOrder.STATE_ING); // 订单状态, 默认订单生成状态
        transferOrder.setAccountNo(bizRQ.getAccountNo()); // 收款账号
        transferOrder.setAccountName(bizRQ.getAccountName()); // 账户姓名
        transferOrder.setBankName(bizRQ.getBankName()); // 银行名称
        transferOrder.setTransferDesc(bizRQ.getTransferDesc()); // 转账备注
        transferOrder.setExtParam(bizRQ.getExtParam()); // 商户扩展参数
        transferOrder.setChannelExtra(bizRQ.getChannelExtra()); // 渠道扩展参数
        transferOrder.setNotifyUrl(bizRQ.getNotifyUrl()); // 异步通知地址
        transferOrder.setDeductionState(TransferOrder.DEDUCTION_STATE_UNHAPPEN); // 转账订单扣款状态

        transferOrderMapper.insert(transferOrder);

        TransferOrderRS bizRes = TransferOrderRS.buildByRecord(transferOrder);

        return ApiRes.okWithSign(bizRes, mchApp.getAppSecret());
    }

}
